package com.yansu.own.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yansu.own.common.dtos.ResponseResult;
import com.yansu.own.common.enums.AppHttpCodeEnum;
import com.yansu.own.entity.pojo.SysUser;
import com.yansu.own.service.SysUserService;
import com.yansu.own.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户层接口
 */
@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("login")
    public ResponseResult<SysUser> login(@RequestBody Map map, HttpSession session) {
        //获取phone参数和验证码code参数
        String phone = (String) map.get("phone");
        //用户填写的验证码
        String code = (String) map.get("code");
        //session中存储的验证码
        //Object rightCode = session.getAttribute(phone);

        //从Redis中获取缓存的验证码
        Object rightCode = redisTemplate.opsForValue().get(phone);
        //验证码比较
        if (rightCode != null && rightCode.equals(code)) {
            //判断用户是否注册
            LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper();
            wrapper.eq(SysUser::getPhone, phone);
            SysUser user = userService.getOne(wrapper);
            log.info("用户id为,{}", user.getUserId());
            if (user == null) { //未注册自动为用户注册
                user = new SysUser();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            log.info("用户id为,{}", user.getUserId());
            session.setAttribute("user", user.getUserId());
            //从Redis中删除缓存的验证码
            redisTemplate.delete(phone);
            return ResponseResult.okResult(user);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.VERIFICATION_CODE_ERROR);
    }

    @PostMapping("/sendMsg")
    public ResponseResult<String> sendMsg(@RequestBody SysUser user, HttpSession session) {
        log.info("phone为{}", user.getPhone());
        //获取phone
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成六位验证码
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
            log.info("code为{}", code);
            //需要将生成的验证码保存到Redis,设置过期时间(5分钟)
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
            return ResponseResult.okResult("验证码发送成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SEND_VERIFICATION_CODE_FAIL);
    }

    //注销用户逻辑
    @PostMapping("/loginOut")
    public ResponseResult<String> loginOut(HttpSession session) {
        session.removeAttribute("user");
        return ResponseResult.okResult();
    }
}
